﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Web.Api.Server.Models;

namespace Web.Api.Server.ViewModels.Shops
{
    public class GoodsPostViewModel : IValidatableObject
    {
        public int? Id { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public string Describe { get; set; }

        [Required]
        public int Inventory { get; set; }

        [Required]
        public decimal Price { get; set; }

        public int? GoodsCategoryId { get; set; }

        [Required]
        public int ShopId { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            var db = validationContext.GetService(typeof(ApplicationDbContext)) as ApplicationDbContext;
            var shop = db.Shops
                .Where(g => g.Id == ShopId)
                .First();
            if (shop == null)
            {
                yield return new ValidationResult("店铺不存在.", new[] { nameof(ShopId) });
            }
        }
    }
}
